package com.azkj.album.adapter

import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import com.azkj.album.R
import com.azkj.album.dto.ListData
import com.azkj.album.network.utils.MyDateUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

class ListAdapter : BaseQuickAdapter<ListData, BaseViewHolder>(R.layout.layout_list_layout) {
    var isRecovery = true
    var selList: List<String> = mutableListOf()
    override fun convert(holder: BaseViewHolder, item: ListData) {
        item.let {
            holder.setText(R.id.tv_time, MyDateUtils.getCurrent(it.createtime.toString()))
            holder.setText(R.id.tv_name, it.albumcategory.name)
            holder.setText(R.id.tv_album, "" + it.images.split(",").size)
            if (TextUtils.isEmpty(it.coverImage)) {
                val images = it.images
                if (!TextUtils.isEmpty(images)) {
                    val split = images.split(",")
                    if (split.isNotEmpty()) {
                        Glide.with(mContext).load(split[0]).into(holder.getView(R.id.iv_image))
                    }
                }
            } else {
                //Glide.with(mContext).load(it.coverImage).placeholder(R.color.white).into(holder.getView(R.id.iv_image))
                Glide.with(mContext).load(it.coverImage).placeholder(R.mipmap.icon_default_img).into(holder.getView(R.id.iv_image))
            }

            holder.setGone(R.id.count_mark, selList.contains(it.id.toString()))
            if (selList.contains(it.id.toString())) {
                holder.setText(R.id.count, (selList.indexOf(it.id.toString()) + 1).toString())
            }
        }
        holder.setGone(R.id.recory_mark, isRecovery && "restore" == item.status)
        if (item.isCollect == 1) {
            holder.getView<ImageView>(R.id.iv_collect).visibility = View.VISIBLE
        } else {
            holder.getView<ImageView>(R.id.iv_collect).visibility = View.INVISIBLE

        }
    }

    fun getItemPosition(item: ListData): Int {
        return data.indexOf(item)
    }

    fun getItemById(id: String): ListData? {
        var item: ListData? = null
        data.let {
            for (i in it) {
                if (id == i.id.toString()) {
                    item = i
                    break
                }
            }
        }
        return item
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }
}